# @Time:   2021/2/25 15:49
# @Author: Zong Wei


from time import sleep

from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class SupplierAccountPeriodPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务通用']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='供方内控账期管理']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")
    close_button = (By.ID, "btn_close")

    supplier_name = (By.ID, "_easyui_textbox_input3")
    supplier_name_value = (By.ID, "datagrid-row-r3-2-0")
    agreement = (By.XPATH, "//select[@id='ParentAgrID']/following::a")
    agreement_value = (By.XPATH, "//*[@id='datagrid-row-r8-2-0']/td[@field='AgreementNo']/div")
    biz_unit = (By.XPATH, "//select[@id='ProductCategoryID']/following::a")
    biz_unit_value = (By.XPATH, "//*[@id='datagrid-row-r6-2-0']/td[@field='ProductCategoryName']/div")
    payment_period = (By.ID, "Days")
    pay_type = (By.ID, "PayType")
    pay_type_value = (By.XPATH, "//div[@id='PayType-option']//li[2]")
    save_period_button = (By.XPATH, "//span[.='保存账期']")
    partner_name = (By.ID, "PartnerNameCN")

    main_agreement = (By.ID, "ParentAgrID")

    period_code = (By.ID, "periodCode")
    period_name = (By.ID, "periodName")
    address = (By.ID, "Address")

    city_value = (By.ID, "datagrid-row-r3-2-0")
    province_id = (By.ID, "ProvinceID")
    city_id = (By.ID, "CityID")
    remark = (By.ID, "Remark")
    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='供方名称']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def account_period_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_period(self, supplier_name='上海音十商务咨询有限公司'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加供方内控账期")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.supplier_name)).send_keys(supplier_name)
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.supplier_name_value)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.agreement)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.agreement_value)).click()
        sleep(1)
        global agreement_num
        agreement_num = self.driver.find_element_by_xpath(
            "//select[@id='ParentAgrID']/following::input[1]").get_property('value')
        print('主协议号：' + agreement_num)
        self.wait.until(ec.element_to_be_clickable(self.biz_unit)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.biz_unit_value)).click()
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.payment_period)).send_keys('90')
        self.wait.until(ec.element_to_be_clickable(self.pay_type)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.pay_type_value)).click()
        self.wait.until(ec.element_to_be_clickable(self.save_period_button)).click()
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.close_button)).click()
        self.logger.info("新增完成")

    def search_period(self, supplier_name='上海音十商务咨询有限公司'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.partner_name)).send_keys(supplier_name)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.main_agreement)).send_keys(agreement_num)
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()
        self.logger.info("查询完成")

    def edit_period(self):
        self.search_period()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.remark)).send_keys(Faker().address())
        sleep(1)
        # self.wait.until(ec.element_to_be_clickable(self.save_period_button)).click()
        # sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.close_button)).click()
        self.logger.info("编辑修改完成")

    def del_period(self):
        self.search_period()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除成功")


if __name__ == '__main__':
    pg = SupplierAccountPeriodPage()
    pg.account_period_home()
    pg.add_period()
